package com.dodo.blog.ui.component.repeater;

import com.dodo.blog.ui.component.container.Panel;
import com.dodo.blog.ui.component.input.Anchor;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class Paginator<T>
        extends Panel
{
    private static final long serialVersionUID = 1L;

    private Anchor firstPage = new Anchor( "first-page", "<<" );
    private Anchor prevPage = new Anchor( "prev-page", "<" );
    private Anchor nextPage = new Anchor( "next-page", ">" );

    private PageIndicator pageIndicator = new PageIndicator();

    public Paginator( String repeaterId )
    {
        setClassName( "paginator" );

        firstPage.setAttribute( Repeater.ATTR_REPEATER_ID, repeaterId );
        firstPage.addClassName( Repeater.CLASS_REPEATER_LINK );
        add( firstPage );

        prevPage.setAttribute( Repeater.ATTR_REPEATER_ID, repeaterId );
        prevPage.addClassName( Repeater.CLASS_REPEATER_LINK );
        add( prevPage );

        pageIndicator.set( 0, 0 );
        add( pageIndicator );

        nextPage.setAttribute( Repeater.ATTR_REPEATER_ID, repeaterId );
        nextPage.addClassName( Repeater.CLASS_REPEATER_LINK );
        add( nextPage );
    }

    public Anchor getFirstPage()
    {
        return firstPage;
    }

    public Anchor getPrevPage()
    {
        return prevPage;
    }

    public Anchor getNextPage()
    {
        return nextPage;
    }

    public PageIndicator getPageIndicator()
    {
        return pageIndicator;
    }

    public class PageIndicator
            extends Panel
    {
        public PageIndicator()
        {
            setClassName( "page-indicator" );
        }

        public void set( int first, int last )
        {
            setInnerHtml( first + "&nbsp;-&nbsp;" + last );
        }
    }
}
